{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bayes' Rule\n",
    "\n",
    "You know that Bayes' rule can be described as a way to improve a prior belief by incorporating observed data, related to this belief (like test data or sensor measurements). The rule is written as: \n",
    "\n",
    "$$P(A|B) = \\frac{P(B|A)\\cdot P(A)} {P(B)}$$\n",
    "\n",
    "Where A is the event and B is some observed, related data.\n",
    "\n",
    "In this next quiz, given only three probabilities: `p_A`, `p_B_given_A`, and `p_notB_given_notA`, which can be written in notation as: \n",
    "\n",
    "$$P(A), P(B|A)$$ \n",
    "$$P(\\neg B|\\neg A)$$ \n",
    "\n",
    "You will be asked to write a function to calculate the posterior probability \n",
    "$$P(A|B)$$\n",
    "\n",
    "*Note*: This is a challenging question, so be sure to test your code or even use a pen and paper to work through this exercise!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### TODO: Complete `bayes`\n",
    "\n",
    "Complete this function so that it returns the posterior probability for any set of inputs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your function returned that the posterior is: 0.3103448275862069\n"
     ]
    }
   ],
   "source": [
    "# Given three input probabilities, complete this function\n",
    "# so that it returns the posterior probability\n",
    "\n",
    "def bayes(p_A, p_B_given_A, p_notB_given_notA):\n",
    "    \n",
    "    ## TODO: Calculate the posterior probability\n",
    "    ## and change this value\n",
    "    posterior = p_A * p_B_given_A / (p_A * p_B_given_A + (1 - p_notB_given_notA) * (1 - p_A))\n",
    "    \n",
    "    return posterior\n",
    "\n",
    "\n",
    "## TODO: Change these values, run your code with them, and use print \n",
    "## statements to see the output of your function and get feedback\n",
    "p_A = 0.2\n",
    "p_B_given_A = 0.9\n",
    "p_notB_given_notA = 0.5\n",
    "\n",
    "posterior = bayes(p_A, p_B_given_A, p_notB_given_notA)\n",
    "print('Your function returned that the posterior is: ' + str(posterior))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Testing Cell\n",
    "\n",
    "Run this cell and it will compare the output of your function with the correct, expected output.\n",
    "\n",
    "**Your code should pass all tests and work for any valid input values.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "That's right!\n"
     ]
    }
   ],
   "source": [
    "# Test code - do not change\n",
    "import solution\n",
    "\n",
    "test_p_A = 0.4\n",
    "test_p_B_given_A = 0.7\n",
    "test_p_notB_given_notA = 0.9\n",
    "\n",
    "# This line calls your function and stores the output\n",
    "posterior = bayes(test_p_A, test_p_B_given_A, test_p_notB_given_notA)\n",
    "correct_posterior = solution.bayes(test_p_A, test_p_B_given_A, test_p_notB_given_notA)\n",
    "\n",
    "# Assertion, comparison test\n",
    "try:\n",
    "    \n",
    "    assert(abs(posterior - correct_posterior) < 0.0001)\n",
    "    print('That\\'s right!')\n",
    "except:\n",
    "    print ('Your code returned that the posterior is: ' +str(posterior) \n",
    "           + ', which does not match the correct value.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
